package net.gyapp.markim.module.basic.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.CallSuper;

import com.easyder.wrapper.base.presenter.MvpBasePresenter;
import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.xuhao.android.libsocket.sdk.ConnectionInfo;
import com.xuhao.android.libsocket.sdk.SocketActionAdapter;
import com.xuhao.android.libsocket.sdk.connection.interfacies.ISocketActionListener;

import net.gyapp.markim.core.ImManager;
import net.gyapp.markim.core.PacketHelper;
import net.gyapp.markim.core.reponse.ImResponse;

import org.jim.common.base.ImPacket;

import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/7/18.
 * Desc:
 */

public abstract class WrapperImActivity<C extends MvpBasePresenter> extends WrapperSwipeActivity<CommonPresenter> {
    protected ISocketActionListener listener;

    @CallSuper
    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        initIm();
    }

    @Override
    protected void onDestroy() {
        if (listener != null) {
            ImManager.getInstance().unRegisterReceiver(listener);
        }
        super.onDestroy();
    }

    protected void initIm() {
        listener = ImManager.getInstance().registerReceiver(new SocketActionAdapter() {
            @Override
            public void onSocketReadResponse(Context context, ConnectionInfo info, String action, ImPacket packet) {
                onMessageReceive(mActivity, info, action, PacketHelper.parsePacket(packet));
            }
        });
    }


    /**
     * 接收到消息回调
     *
     * @param context
     * @param info
     * @param action
     * @param response
     */
    public abstract void onMessageReceive(Context context, ConnectionInfo info, String action, ImResponse response);


}
